package com.qymerge.manage.data.service.impl;

import com.qymerge.manage.base.dao.IProductDao;
import com.qymerge.manage.base.dao.IUserWXDao;
import com.qymerge.manage.base.ro.ProductRO;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.data.dao.IXFRecordDao;
import com.qymerge.manage.data.dto.XFRecordDto;
import com.qymerge.manage.data.dto.export.XFRecordExportDTO;
import com.qymerge.manage.data.ro.XFRecordRO;
import com.qymerge.manage.data.service.IXFRecordService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: XFRecordServiceImpl</p>
 *<p>Description: 消费记录</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19/0019
 *@Time 17:47
 */
@Service
public class XFRecordServiceImpl implements IXFRecordService {

    @Autowired
    private IXFRecordDao xFRecordDao;

    @Autowired
    private IUserWXDao userWXDao;

    @Autowired
    private IProductDao productDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(XFRecordDto ro, PageRequest pageRequest) {
        List<XFRecordRO> list = xFRecordDao.list(ro, pageRequest);

        for (XFRecordRO xfRecordRO : list) {
            // 设置用户信息
            long userwxid = xfRecordRO.getUserwxid();
            if (userwxid > 0) {
                xfRecordRO.setUser_info(userWXDao.findOne(userwxid));
            }

            // 设置产品信息
            long productid = xfRecordRO.getProductid();
            if (productid > 0) {
                xfRecordRO.setProduct_info(productDao.findOne(productid));
            }
        }

        DataTableRO dataTableRO = new DataTableRO();
        int allCount = xFRecordDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public XFRecordRO detail(Long id) {
        XFRecordRO xfRecordRO = xFRecordDao.findOne(id);
        // 设置用户信息
        long userwxid = xfRecordRO.getUserwxid();
        if (userwxid > 0) {
            xfRecordRO.setUser_info(userWXDao.findOne(userwxid));
        }
        // 设置产品信息
        long productid = xfRecordRO.getProductid();
        if (productid > 0) {
            xfRecordRO.setProduct_info(productDao.findOne(productid));
        }
        return xfRecordRO;
    }

    /**
     * 新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO save(XFRecordRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = xFRecordDao.install(ro);
        } else {
            rn = xFRecordDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = xFRecordDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"成功", "删除" + rn +"失败");
    }

    /**
     * 导出表格
     *
     * @param ro
     * @return
     */
    @Override
    public List<XFRecordExportDTO> exportData(XFRecordDto ro) {
        List<XFRecordExportDTO> list = new ArrayList<XFRecordExportDTO>();
        XFRecordExportDTO dto = null;

        List<XFRecordRO> xfRecordROS = xFRecordDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(xfRecordROS)) {
            for (XFRecordRO xfRecordRO : xfRecordROS) {
                dto = new XFRecordExportDTO();

                dto.setId(xfRecordRO.getId());
                dto.setUserwxid(xfRecordRO.getUserwxid());
                dto.setProductid(xfRecordRO.getProductid());

                // 设置产品信息
                long productid = xfRecordRO.getProductid();
                if (productid > 0) {
                    ProductRO productRO = productDao.findOne(productid);
                    dto.setProductname(productRO.getProductname());
                } else {
                    dto.setProductname("");
                }

                dto.setMoney(xfRecordRO.getMoney());
                dto.setNum(xfRecordRO.getNum());
                dto.setCreatedate(xfRecordRO.getCreatedate());

                list.add(dto);
            }
        }

        return list;
    }

}
